<template>
    <view class="container">
        <public-module leftIcon="arrow-left" :title="title" />
        
        <!-- 训练模式选择 -->
        <view class="mode-selection">
            <view class="mode-container">
                <view v-for="(row, rowIndex) in modeRows" :key="rowIndex" class="mode-row">
                    <view v-for="mode in row" :key="mode.index" class="mode-item"
                        :class="{ 'selected': selectedMode === mode.index }" @click="selectMode(mode.index)">
                        <text class="mode-title">{{ mode.title }}</text>
                    </view>
                    <!-- 在行未填满时添加占位元素 -->
                    <!-- 注意：在非H5平台，key不能使用表达式，所以使用index作为key -->
                    <view v-for="(item, index) in getEmptyItems(row.length)" :key="index" class="mode-item empty"></view>
                </view>
            </view>
            
            <!-- 训练设置区域 -->
            <view class="training-settings">
                <view class="settings-section">
                    <view class="setting-row">
                        <text class="section-title">选择数量</text>
                        <view class="option-row">
                            <view v-for="count in questionCounts" :key="count" class="option-item"
                                :class="{ 'selected': selectedQuestionCount === count }" @click="selectQuestionCount(count)">
                                <text>{{ count }}题</text>
                            </view>
                        </view>
                    </view>
                </view>

                <view class="settings-section">
                    <view class="setting-row">
                        <text class="section-title">选择误差</text>
                        <view class="option-row">
                            <view v-for="range in errorRanges" :key="range" class="option-item"
                                :class="{ 'selected': selectedErrorRange === range }" @click="selectErrorRange(range)">
                                <text>±{{ range }}%</text>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
            
            <button class="start-btn" @click="startTrainingMode">开始训练</button>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: '估算训练',
                
                // 训练设置
                questionCounts: [10, 20, 50],
                selectedQuestionCount: 10,
                errorRanges: [1, 2, 3, 4, 5],
                selectedErrorRange: 5,
                
                trainingModes: [{
                        title: '两位数加减',
                        type: 'twoDigitAddSub'
                    },
                    {
                        title: '三位数加减',
                        type: 'threeDigitAddSub'
                    },
                    {
                        title: '两位数相乘',
                        type: 'twoDigitMultiply'
                    },
                    {
                        title: '两位数乘一位数',
                        type: 'twoDigitOneDigitMultiply'
                    },
                    {
                        title: '三位数除一位数',
                        type: 'threeDigitOneDigitDivide'
                    },
                    {
                        title: '三位数乘百分数',
                        type: 'threeDigitPercentage'
                    },
                    {
                        title: '30以内平方数',
                        type: 'squareWithin30'
                    },
                    {
                        title: '两位数除三位数',
                        type: 'twoDigitDivideThreeDigit'
                    },
                    {
                        title: '五位数除三位数',
                        type: 'fiveDigitDivideThreeDigit'
                    },
                    {
                        title: '三位数除两位数',
                        type: 'threeDigitDivideTwoDigit'
                    }
                ],
                selectedMode: 0
            };
        },
        
        computed: {
            // 将模式数组转换为2列布局
            modeRows() {
                const rows = [];
                const modesPerRow = 2; // 改为2列
                for (let i = 0; i < this.trainingModes.length; i += modesPerRow) {
                    const row = [];
                    for (let j = 0; j < modesPerRow && i + j < this.trainingModes.length; j++) {
                        const mode = this.trainingModes[i + j];
                        row.push({
                            ...mode,
                            index: i + j
                        });
                    }
                    rows.push(row);
                }
                return rows;
            }
        },
        
        methods: {
            selectMode(index) {
                this.selectedMode = index;
            },
            
            // 获取空占位元素的数量，解决非H5平台key不能使用表达式的问题
            getEmptyItems(rowLength) {
                return Array.from({length: 2 - rowLength}); // 改为2列布局
            },
            
            // 选择题目数量
            selectQuestionCount(count) {
                this.selectedQuestionCount = count;
            },
            
            // 选择误差范围
            selectErrorRange(range) {
                this.selectedErrorRange = range;
            },
            
            // 开始训练
            startTrainingMode() {
                const mode = this.trainingModes[this.selectedMode];
                
                // 跳转到统一的训练页面
                uni.navigateTo({
                    url: `/pages/calculation/training/index?type=basic&mode=${mode.type}&modeTitle=${encodeURIComponent(mode.title)}&questionCount=${this.selectedQuestionCount}&errorRange=${this.selectedErrorRange}`
                });
            }
        }
    };
</script>

<style>
    .container {
        display: flex;
        flex-direction: column;
        background-color: #f8f8f8;
        min-height: 100vh;
        box-sizing: border-box;
    }

    .mode-selection {
        padding: 15px;
    }

    .mode-container {
        width: 100%;
        margin-bottom: 20px;
    }

    .mode-row {
        display: flex;
        width: 100%;
        margin-bottom: 8px;
    }

    .mode-item {
        flex: 1;
        margin-right: 8px;
        padding: 12px 8px;
        border-radius: 8px;
        text-align: center;
        font-size: 14px;
        background-color: #ffffff;
        border: 1px solid #e9ecef;
        transition: all 0.2s ease;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        box-sizing: border-box;
    }

    .mode-item:last-child {
        margin-right: 0;
    }

    .mode-item.empty {
        background: transparent;
        border: none;
        box-shadow: none;
        pointer-events: none;
    }

    .mode-item.selected {
        color: #2e7d32;
        font-weight: bold;
        background-color: #e8f5e8;
        border-color: #4CAF50;
    }

    /* 训练设置区域样式 */
    .training-settings {
        display: flex;
        flex-direction: column;
        gap: 15px;
        margin-bottom: 20px;
    }

    .settings-section {
        background-color: #ffffff;
        border-radius: 8px;
        padding: 15px;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
    }

    .setting-row {
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .section-title {
        font-size: 14px;
        font-weight: 600;
        color: #333;
        flex-shrink: 0;
        width: 70px;
        text-align: left;
    }

    .option-row {
        display: flex;
        gap: 6px;
        flex: 1;
        overflow-x: auto;
    }

    .option-item {
        padding: 6px 6px;
        border-radius: 6px;
        background-color: #f8f9fa;
        border: 1px solid #e9ecef;
        transition: all 0.2s ease;
        cursor: pointer;
        flex-shrink: 0;
        text-align: center;
        font-size: 13px;
        white-space: nowrap;
    }

    .option-item.selected {
        background-color: #e8f5e8;
        border-color: #4CAF50;
        color: #2e7d32;
        font-weight: 600;
    }

    .start-btn {
        width: 200px;
        padding: 12px;
        background-color: #4CAF50;
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 16px;
        font-weight: 600;
        margin: 10px auto 0;
        display: block;
    }
</style>